<template>
    <div>
        <div class="card-list">

            <!-- 楼层标题 -->
            <header class="storey-title">

                <div class="s-t-left">
                    <i class="iconfont icon-donghua"></i>
                    <div>{{ mdname }}</div>
                </div>

                <div class="s-t-right">
                    <div class="btn-change">
                        <i class="iconfont icon-huanyihuan"></i>
                        <span @click="changeIt"> 换一换</span>
                    </div>
                    <div class="more">
                        <a href="javascript:;" @click="promptMessage">
                            更多
                            <i class="iconfont icon-arrow-right"></i>
                        </a>
                    </div>
                </div>
            </header>

            <!-- 楼层内容 -->
            <div class="storey-content">
                <div class="s-c-item" v-for="(item, i) in maindata" :key="i" @click="jumpPath(item.id)">

                    <div class="item-pic">
                        <!-- 3.图片懒加载修改img :src -> v-lazy -->
                        <a :href="'/o/video/VideoDetail/' + item.id">
                            <img v-lazy="downloadUrl + item.videoCover" alt="" />
                            <div class="duration">
                                {{ timeHandle(item.videoDuration) }}
                            </div>
                            <div class="count">
                                <i class="iconfont icon-bofangliang1"></i>
                                {{ tenThousand(item.videoPlay) }}
                                <i class="iconfont icon-dianzan"></i>
                                {{ tenThousand(item.videoPlay) }}
                            </div>
                        </a>
                    </div>

                    <div class="item-title">
                        <a href="">{{ item.videoTitle }}</a>
                    </div>

                    <div class="item-up" @click="routerJump('/c/user/userCenter?userId=' + item.userId)">
                        <img v-if="item.userCover" class="up-cover" v-lazy="downloadUrl + item.userCover" alt="" />
                        <i v-else class="iconfont icon-icon_bilibili-circle up-cover"></i>
                        <span v-if="item.nickName">{{ item.nickName }}</span>
                        <span v-else>{{ item.userName }}</span>
                    </div>

                </div>
            </div>

        </div>
    </div>
</template>

<script>
export default {
    name: "Card",
    props: {
        maindata: {
            type: Array,
            default() {
                return [];
            },
        },
        mdname: {
            type: String,
            default: "",
        },
    },
    data() {
        return {
            downloadUrl: process.env.VUE_APP_DOWNLOAD_URL,
        };
    },
    created() {

    },
    mouthed() {
        this.getName();
    },
    computed: {
        // 计算数量过万就转换
        tenThousand() {
            // 计算属性是一个属性不是一个方法, 使用函数返回
            return function (count) {
                if (count >= 100000000) {
                    return (count / 100000000).toFixed(1) + "亿";
                }
                return count >= 10000 ? (count / 10000).toFixed(1) + "万" : count;
            };
        },
    },

    methods: {
        // 获取标题名字
        getName() {
            // 如果 this.maindata 里面有 tname 这个属性就 return ture取反(false)
            // every() 如果数组中检测到有一个元素不满足，则整个表达式返回 false ，且剩余的元素不会再进行检测。
            if (this.maindata.every((item) => !("tname" in item))) {
                return;
            }
            return this.maindata[0].tname;
        },

        // 点击换一换
        changeIt() {
            // 事件总线 发射:this.$bus.$emit('aaaa')  接收:this.$bus.$on('aaaa',function)
            this.$bus.$emit("changeList", this.mdname);
        },

        // 处理时间
        timeHandle(time) {
            if (isNaN(parseFloat(time)) || !isFinite(time)) { return time; }
            let timeFormat = "";
            let isFirst = 0;
            function allTime(time) {
                if (time < 60) {
                    let ss;
                    if (time > 0) {
                        if (time < 10) {
                            time = "0" + time;
                        }
                        ss = isFirst == 0 ? "0:" + time : time;
                    } else if (time == 0) {
                        ss = "00"
                    } else { ss = "" }
                    timeFormat = timeFormat + ss;
                    return;
                } else if (time < 3600) {
                    let mm = Math.floor(time / 60);
                    mm = mm <= 10 & isFirst > 0 ? "0" + mm : mm;
                    isFirst++;
                    timeFormat = timeFormat + mm + ":";
                    allTime(time - mm * 60);
                } else {
                    let hh = Math.floor(time / 3600);
                    hh = hh <= 10 ? "0" + hh : hh;
                    isFirst++;
                    timeFormat = timeFormat + hh + ":";
                    allTime(time - hh * 3600);
                }
            }
            allTime(time);
            return timeFormat;
        },

        // 点击跳转
        jumpPath(id) {
            this.$router.push(`/video/VideoDetail/${id}`);
        },
        routerJump(id) {
            this.$router.push(`${id}`);
        },

        // 提示信息
        promptMessage() {
            this.$message.warning("此页面还没有做好哦!");
        },
    },
};
</script>

<style scoped>
.card-list {
    width: 100%;
    height: 100%;
    font: 12px Helvetica Neue, Helvetica, Arial, Microsoft Yahei, Hiragino Sans GB,
        Heiti SC, WenQuanYi Micro Hei, sans-serif;
}

/* 楼层标题区域 */
.storey-title {
    padding: 10px;
    display: flex;
    justify-content: space-between;
}

.s-t-left {
    display: flex;
    /* align-items: center; */
    font-size: 20px;
}

.s-t-left i {
    font-size: 25px;
    color: #7b78eb;
    margin-right: 8px;
}

.s-t-right {
    display: flex;
    justify-content: space-around;
    align-items: center;
    text-align: center;
    width: 140px;
    font-size: 14px;
}

.s-t-right .btn-change {
    flex: 1;
    margin-right: 5px;
    padding: 2px 5px;
    border-radius: 5px;
    border: 1px solid #ccc;
    cursor: pointer;
}

.btn-change:hover {
    background: #f5f5f5;
}

.s-t-right .more {
    padding: 2px 5px;
    border-radius: 5px;
    border: 1px solid #ccc;
}

/* 楼层内容区域 */
.storey-content {
    width: 100%;
    height: 95%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
}

.s-c-item {
    width: 19%;
    height: 45%;
    /* margin-bottom: 10px; */
}

.item-pic {
    position: relative;
    width: 100%;
    height: 60%;
    border-radius: 5px;
    overflow: hidden;
    /* box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); */
}

.item-pic img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    /* overflow: hidden; */
}

.item-pic .count {
    position: absolute;
    left: 0;
    bottom: 0;
    font-size: 12px;
    padding: 3px 5px;
    color: #fff;
    background-image: linear-gradient(to top,
            rgba(0, 0, 0, 0.6),
            rgba(119, 119, 119, 0.1));
    overflow: hidden;
}

.duration {
    position: absolute;
    right: 0;
    top: 0;
    font-size: 14px;
    padding: 3px 5px;
    color: #fff;
    background-image: linear-gradient(to left bottom,
            rgba(0, 0, 0, 0.6),
            rgba(255, 255, 255, 0.1));
    border-bottom-left-radius: 8px;
}

.item-title {
    height: 40px;
    line-height: 20px;
    margin-top: 5px;
    margin-bottom: 5px;
    font-size: 14px;
    overflow: hidden;
    word-break: break-all;
}

.item-title a {
    &:hover {
        color: #52aae2;
    }
}

.item-up {
    margin-left: 5px;
    display: flex;
    align-items: center;
    /* justify-content: center; */
    cursor: pointer;
}

.up-cover {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin-right: 5px;
}
</style>